package com.leyou.exception;

import com.leyou.Parameter;
import com.leyou.ResponseData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class CommonExceptionHandler {

    @ExceptionHandler(SysException.class)
    public ResponseEntity<ResponseData> handlerException(SysException e) {
        ExceptionEnum en = e.getExceptionEnum();
        Parameter parameter = e.getParameter();
        ResponseData data = new ResponseData(parameter, null);
        if (en != null) {
            data.setMsg(en.msg());
            data.setStatus(en.status());
        }
        if (StringUtils.isNotBlank(e.getMsg())) {
            data.setMsg(e.getMsg());
        }
        if (e.getStatus() > 0) {
            data.setStatus(e.getStatus());
        }

        return ResponseEntity.ok().body(data);
    }

}
